package com.piggy.logi.sys.controller;

import com.piggy.common.core.domain.R;
import com.piggy.common.core.utils.poi.ExcelUtil;
import com.piggy.common.core.web.controller.BaseController;
import com.piggy.common.core.web.page.TableDataInfo;
import com.piggy.common.log.annotation.Log;
import com.piggy.common.log.enums.BusinessType;
import com.piggy.logi.sys.bo.LogiWaybillLogEditBo;
import com.piggy.logi.sys.bo.LogiWaybillLogQueryBo;
import com.piggy.logi.sys.service.ILogiWaybillLogService;
import com.piggy.logi.sys.vo.LogiWaybillLogVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;

/**
 * 运单运行日志Controller
 *
 * @author piggy
 * @date 2023-12-07
 */
@Api(value = "运单运行日志控制器", tags = {"运单运行日志管理"})
@RequiredArgsConstructor
@RestController
@RequestMapping("/sys/billlog")
public class LogiWaybillLogController extends BaseController {

    private final ILogiWaybillLogService iLogiWaybillLogService;

    /**
     * 查询运单运行日志列表
     */
    @ApiOperation("查询运单运行日志列表")
    //@SaCheckPermission("sys:billlog:list")
    @PostMapping("/list")
    public TableDataInfo<LogiWaybillLogVo> list(@Validated @RequestBody LogiWaybillLogQueryBo bo) {
        return iLogiWaybillLogService.queryPageList(bo);
    }

    /**
     * 导出运单运行日志列表
     */
    @ApiOperation("导出运单运行日志列表")
    //@SaCheckPermission("sys:billlog:export")
    @Log(title = "运单运行日志", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public void export(@Validated LogiWaybillLogQueryBo bo, HttpServletResponse response) throws IOException {
        List<LogiWaybillLogVo> list = iLogiWaybillLogService.queryList(bo);
        ExcelUtil<LogiWaybillLogVo> util = new ExcelUtil<LogiWaybillLogVo>(LogiWaybillLogVo.class);
        util.exportExcel(response, list, "运单运行日志");
    }

    /**
     * 获取运单运行日志详细信息
     */
    @ApiOperation("获取运单运行日志详细信息")
    //@SaCheckPermission("sys:billlog:query")
    @GetMapping("/{id}")
    public R<LogiWaybillLogVo> getInfo(@NotNull(message = "主键不能为空")
                                                  @PathVariable("id") Long id) {
        return R.ok(iLogiWaybillLogService.queryById(id));
    }

    /**
     * 新增运单运行日志
     */
    @ApiOperation("新增运单运行日志")
    //@SaCheckPermission("sys:billlog:add")
    @Log(title = "运单运行日志", businessType = BusinessType.INSERT)
    @PostMapping()
    public R<LogiWaybillLogVo> add(@Validated @RequestBody LogiWaybillLogEditBo bo) {
        return R.ok(iLogiWaybillLogService.insertByAddBo(bo));
    }

    /**
     * 修改运单运行日志
     */
    @ApiOperation("修改运单运行日志")
    //@SaCheckPermission("sys:billlog:edit")
    @Log(title = "运单运行日志", businessType = BusinessType.UPDATE)
    @PutMapping()
    public R<LogiWaybillLogVo> edit(@Validated @RequestBody LogiWaybillLogEditBo bo) {
        return R.ok(iLogiWaybillLogService.updateByEditBo(bo));
    }

    /**
     * 删除运单运行日志
     */
    @ApiOperation("删除运单运行日志")
    //@SaCheckPermission("sys:billlog:remove")
    @Log(title = "运单运行日志" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Integer> remove(@NotEmpty(message = "主键不能为空")
                                       @PathVariable Long[] ids) {
        return R.ok(iLogiWaybillLogService.deleteWithValidByIds(Arrays.asList(ids), true) ? 1 : 0);
    }
}
